package team.se.tms.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.se.tms.constant.RoleConstant;
import team.se.tms.dao.impl.DaoImpl;
import team.se.tms.dto.Page;
import team.se.tms.factory.ModelFactory;
import team.se.tms.po.TeacherInfo;
import team.se.tms.po.UserInfo;
import team.se.tms.service.TeacherInfoService;
import team.se.tms.util.KeyUtil;

import java.util.List;

/**
 * 教师信息业务层，具体实现教师信息业务接口层定义的方法
 *
 * @author qingx
 * @date 2018/11/28
 */
@Slf4j
@Service("TeacherInfoServiceImpl")
public class TeacherInfoServiceImpl implements TeacherInfoService {

    @Autowired
    public DaoImpl dao;


    /**
     * 获取全部教师信息
     *
     * @return 教师信息
     * @throws Exception 异常
     * @create on 2018/11/28 by qingx
     */
    @Override
    public List<TeacherInfo> listAllTeacherInfos(Page page) throws Exception {
        System.out.println("1111111111111111111111111111111111"+dao.selectList("TeacherInfoMapper.selectAllTeacherInfos",page));
        return dao.selectList("TeacherInfoMapper.selectAllTeacherInfos",page);
    }

    /**
     * 录入教师信息
     *
     * @return 教师信息
     * @throws Exception 异常
     * @create on 2018/12/2 by qingx
     */
    @Override
    public String savaTeacherInfo(String addType, TeacherInfo teacherInfo) throws Exception {
        int updateResult = 0;
        UserInfo user = new UserInfo();
        if(addType.equals("addNewUser")){
            //生成用户实例
            user = ModelFactory.createUserInfo(RoleConstant.TEACHER);
            teacherInfo.setUserId(user.getUserId());
            System.out.println("******************************************"+user);
            updateResult = (int) dao.insert("UserInfoMapper.insertUserInfo",user);
        }else if(addType.equals("addExistingUser")){
            user.setUserId(teacherInfo.getUserId());
            user.setRoleId(RoleConstant.TEACHER);
            System.out.println("******************************************"+user);
            updateResult = (int) dao.update("UserInfoMapper.updateUserInfoByUserId",user);
        }
        if(updateResult != 0){
            int result = (int) dao.insert("TeacherInfoMapper.insertTeacherInfo", teacherInfo);
            if(result != 0){
                return teacherInfo.getUserId();
            }
        }
        return null;
    }

    /**
     * 修改教师信息
     *
     * @param teacherInfo 教师信息
     * @return
     * @throws Exception
     * @create on 2018/12/7 by qingx
     */
    @Override
    public boolean modifyTeacherInfo(TeacherInfo teacherInfo) throws Exception {
        int result = dao.update("TeacherInfoMapper.updateTeacherInfoByTeaId", teacherInfo);
        if (result != 0){
            return true;
        }
        return false;
    }

    /**
     * 查看教师信息
     *
     * @param userId 参数描述
     * @return TeacherInfo 教师信息
     * @throws Exception 异常
     * @create on 2018/12/9 by johnny
     */
    @Override
    public TeacherInfo getTeacherInfo(String userId) throws Exception {
        return dao.selectOne("TeacherInfoMapper.selectTeacherInfoByUserId",userId);
    }


}
